// 依赖和基础信息
const ethers = require('ethers')
const Token = require('../Token.json')

const ganachePrivateKey =
  '0x700c472fef576757270df5068e0ec7152e7307000cfe3fbf5d15be43bde55230'

// 连接provider
let local_provider = new ethers.providers.JsonRpcProvider(
  'http://localhost:8545',
)

// 账户
let wallet = new ethers.Wallet(ganachePrivateKey, local_provider)
console.log(`Loaded wallet ${wallet.address}`)

// 合约
const abi = Token.abi
const bytecode = Token.bytecode

// 部署合约
async function deploy() {
  // 构造合约
  let contractFactory = new ethers.ContractFactory(abi, bytecode, wallet)

  // 部署合约 传入参数：具体所需要的参数参见.sol文件源码
  let contract = await contractFactory.deploy(10000)
  console.log('智能合约部署交易hash', contract.deployTransaction.hash)
  console.log('智能合约地址:', contract.address)

  //等待合约部署完成
  await contract.deployed()

  console.log('------合约部署成功')
  return contract.address
}

deploy()
